#include "view_time_21.h"
#include "view_time_21.inc"
#include "view_time_14.h"
#include "view_time_3.h"
#include "view_time_0.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "font_mngr.h"
#include "view_common.h"
#include "app_trace.h"
#include "view_times.h"
#include "math.h"
#include "weather_ctrl.h"
#include "health_storage.h"
#include "app_trace.h"
#define LOG(fmt, ...) app_trace_log(fmt "\r\n", ##__VA_ARGS__)
#ifdef TIME_21_SKIP
#else

static void show_view_time_show_temperature_float_with_font(int x, int y, float temperature, bool is_centigrade, bool is_center_align, const unsigned char *const *image_tab, int font_interval,
                                                            int point_interval, int unit_interval)
{
    Ensures(temperature >= 0);

    // const unsigned char *const *image_tab = num_16x18_TAB;
    int const font_width = image_tab[0][0] + (image_tab[0][1] << 8);
    int const font_height = image_tab[0][2] + (image_tab[0][3] << 8);
    int const interval = font_interval;
    int draw_x = 0;
    if (isnan(temperature))
    {
        if (is_center_align)
        {
            x += 26;
        }
        disp_draw_rect_color(x - 5, y + font_height / 2 - 2, x + font_width - 6, y + font_height / 2, RGB565_COLOR_WHITE);
        x += font_width + interval;
        disp_draw_rect_color(x - 2, y + font_height / 2 - 2, x + font_width - 3, y + font_height / 2, RGB565_COLOR_WHITE);
        return;
    }
    if (!is_centigrade)
    {
        temperature = 32.f + temperature * 1.8f;
    }
    // float interger_part = 0.f;
    // float decimal_part = 0.f;

    // decimal_part = modff(temperature, &interger_part);
    int temperature_int = temperature * 100;
    if (temperature < 0)
    {
        disp_draw_rect_color(x, y + font_height / 2 - 1, x + font_width - 1, y + font_height / 2, RGB565_COLOR_WHITE);
        x += font_width + interval;
    }
    draw_x = disp_draw_user_image_num_filter_color(x, y, temperature_int / 100, font_width + interval, image_tab);
    draw_x -= interval;
    draw_x += point_interval;
    disp_draw_rect_color(draw_x, y + font_height - 3, draw_x + 3 - 1, y + font_height - 1, RGB565_COLOR_WHITE);
    draw_x += 3 + point_interval;
    draw_x = disp_draw_user_image_num_with_digits_filter_color(draw_x, y, temperature_int % 100, 2, font_width + interval, image_tab);
    draw_x -= interval;
    draw_x += unit_interval;
    // draw_x += 6;
    if (is_centigrade)
    {
        disp_draw_user_image_filter_color(draw_x, y, img_time_21_celsius);
    }
    else
    {
        disp_draw_user_image_filter_color(draw_x, y, img_time_21_fahrenheit);
    }
}

static void view_time_21_week(void)
{
    disp_draw_user_image_filter_color(111, 37, img_time_21_week_TAB[disp_ctrl.com_data.i_temp[5] - 1]);
}

static void view_time_21_date(void)
{
    disp_draw_user_image_filter_color(123, 81, img_time_21_date_TAB[disp_ctrl.com_data.i_temp[2] / 10]);
    disp_draw_user_image_filter_color(162, 82, img_time_21_date_TAB[disp_ctrl.com_data.i_temp[2] % 10]);
}

static void view_time_21_temperature(void)
{
    float temperature = disp_ctrl.com_data.f_temp[26];
    show_view_time_show_temperature_float_with_font(19, 49, temperature, disp_ctrl.com_data.i_temp[21] == WEATHER_TEMPERATURE_TYPE_C, true, img_time_21_num_TAB, 1, 1, 1);
}

static void view_time_21_back(void)
{
    const uint8_t img_time_21_back = FILE_IMG_back_13_9;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_4, 14, 9, img_time_21_back);
}

static void view_time_21_steps(void)
{
    int steps = disp_ctrl.com_data.i_temp[11];
    disp_draw_user_image_num_mid_gray_lucency(50, 122, RGB565_COLOR_WHITE, steps, 11, img_time_21_num_TAB);
}

static void view_time_21_time(void)
{
    const uint8_t img_time_21_time_TAB[] = {
        FILE_IMG_num_45_62_0, FILE_IMG_num_45_62_1, FILE_IMG_num_45_62_2, FILE_IMG_num_45_62_3, FILE_IMG_num_45_62_4,
        FILE_IMG_num_45_62_5, FILE_IMG_num_45_62_6, FILE_IMG_num_45_62_7, FILE_IMG_num_45_62_8, FILE_IMG_num_45_62_9,
    };

    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 18, 168, img_time_21_time_TAB[disp_ctrl.com_data.i_temp[3] / 10]);
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 69, 168, img_time_21_time_TAB[disp_ctrl.com_data.i_temp[3] % 10]);
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, 135, 168, img_time_21_time_TAB[disp_ctrl.com_data.i_temp[4] / 10]);
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_3, 185, 168, img_time_21_time_TAB[disp_ctrl.com_data.i_temp[4] % 10]);

    disp_draw_user_image(117, 168, img_time_21_colon);
}

void view_time_21_display(void)
{

    view_time_21_back();
    view_time_21_week();
    view_time_21_date();
    view_time_21_temperature();
    view_time_21_steps();
    view_time_21_time();
}
#endif
